package cn.ffcs.uec.outStaff.action;

import java.text.ParseException;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.common.util.NodeVoUtil;
import cn.ffcs.uec.common.util.UecConstants;
import cn.ffcs.uec.outStaff.action.bean.UecOutStaffPunishEditBean;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.staff.model.UecPunish;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;

@Controller
@Scope("prototype")
public class UecOutStaffPunishEditComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private UecOutStaffPunishEditBean bean = new UecOutStaffPunishEditBean();
	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 人员信息
	 */
	private UecOutStaffInfo uecOutStaffInfo;
	/**
	 * 添加政治面貌
	 */
	private UecPunish uecPunish;

	/**
	 * 是否由 入职页面 引用
	 */
	private boolean isOutStaffEntryExt;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化窗口
	 * 
	 * @throws ParseException
	 */
	public void onCreate$uecPunishEditWindow() throws ParseException {
		this.bindCombobox();
		this.bindBean();
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {
		// 处罚级别
		ListboxUtils.rendererForEdit(bean.getGlbdef4(),
				NodeVoUtil.getDictionaries(UecConstants.PUNISH_LEVEL));
	}

	/**
	 * bindBean
	 * 
	 * @throws ParseException
	 */
	public void bindBean() throws ParseException {
		isOutStaffEntryExt = (Boolean) arg.get("isOutStaffEntryExt");
		opType = (String) arg.get("opType");
		if ("add".equals(opType)) {
			this.bean.getUecPunishEditWindow().setTitle("新增处罚情况信息");
			bean.getUecPunishEditDiv().setHeight("30%");
			uecOutStaffInfo = (UecOutStaffInfo) arg.get("uecOutStaffInfo");
		}
		if ("view".equals(opType)) {
			this.bean.getUecPunishEditWindow().setTitle("查看处罚情况信息");
			uecPunish = (UecPunish) arg.get("uecPunish");
			bean.getCancelBtn().setVisible(false);
			bean.getSaveBtn().setVisible(false);
			if (null != uecPunish) {
				PubUtil.fillBeanFromPo(uecPunish, bean);
				stringTransToDate();
				bean.getButtonDiv().setVisible(false);
				PubUtil.disableComponent(bean.getUecPunishEditDiv(), true);
			}
		}
		if ("edit".equals(opType)) {
			this.bean.getUecPunishEditWindow().setTitle("编辑处罚情况信息");
			bean.getUecPunishEditDiv().setHeight("30%");
			uecPunish = (UecPunish) arg.get("uecPunish");
			if (null != uecPunish) {
				PubUtil.fillBeanFromPo(uecPunish, bean);
				stringTransToDate();
			}
		}
		if ("approvalEdit".equals(opType)) {
			this.bean.getUecPunishEditWindow().setTitle("审批处罚情况信息修改");
			bean.getUecPunishEditDiv().setHeight("30%");
			uecPunish = (UecPunish) arg.get("uecPunish");
			if (null != uecPunish) {
				PubUtil.fillBeanFromPo(uecPunish, bean);
				stringTransToDate();
			}
		}
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		// 获取登录用户
		String currentName = "";
		if (StrUtil.isNullOrEmpty(PlatformUtil.getCurrentUser())) {
			ZkUtil.showError("当前登录用户已经失效，请重新登录系统", "错误提示");
			return;
		} else {
			currentName = PlatformUtil.getCurrentUser().getScreenName();
		}
		if ("add".equals(opType)) {
			uecPunish = UecPunish.newInstance();
			PubUtil.fillPoFromBean(bean, uecPunish);
			dataTransToString();
			uecPunish.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			uecPunish.setStaffId(uecOutStaffInfo.getOutStaffId());
			uecPunish.setPkPsndoc(uecOutStaffInfo.getPkPsnMsg());
			// 删除标志
			uecPunish.setDr(0L);
			uecPunish.setLastflag("Y");
			uecPunish.setCreator(currentName);
			uecPunish.setCreationtime(DateUtil.getCurrenDate());

			if (!isOutStaffEntryExt) {
				uecPunish.addOnly();
			}

			Events.postEvent("onOK", this.self, uecPunish);
			this.onCancel();
			Messagebox.show("新增成功！");

			// 发起流程 uec_punish 新增一条记录
			// String procDefId =
			// "outStaffPunishEdit:1:7544019d30414974afd4f2d514d481b0";
			// 判断该人员是否在审状态，如果是在审状态不允许再次发起此流程
			// boolean isOnApproval = ApprovalUtil.isOnApproval(procDefId,
			// uecOutStaffInfo.getOutStaffId());
			// if (isOnApproval) {
			// ZkUtil.showError("在审状态不允许发起！", "错误提示");
			// return;
			// }

			// Long outStaffId = uecOutStaffInfo.getOutStaffId();
			//
			// uecPunish.addStatusCdIS3();
			// Long punishId = uecPunish.getPunishId();
			//
			// if (StrUtil.isNullOrEmpty(PlatformUtil.getCurrentUser())) {
			// ZkUtil.showError("当前登录用户不存在!", "错误提示");
			// return;
			// }
			// String currentName = PlatformUtil.getCurrentUser()
			// .getScreenName();
			//
			// String startFlowJson = "{" + "formTheme: '盟市外包人员惩罚申请流程',"
			// + " formContent: '惩罚人员:"
			// + uecOutStaffInfo.getPsnName()
			// + "',"
			// + " procDefId: '"
			// + procDefId
			// + "',"
			// + " urgent: '1',"
			// + " atiActCategoryId: '21',"
			// + " procDefKey: '"
			// + ApprovalUtil.OUT_STAFF_PUNISH_EDIT
			// + "',"
			// + " formSender: '"
			// + currentName
			// + "',"
			// + " primaryId: '{"
			// + "uec_out_staff_info_his:"
			// + outStaffId + ",uec_out_staff_info:" + outStaffId
			// + ",uec_punish:" + punishId + "'}";
			//
			// String procInstId = null;
			// try {
			// procInstId = ApprovalUtil.startApproval(startFlowJson);
			// } catch (Exception e) {
			// // 流程发起失败 数据改为失效
			// uecPunish.removeOnly();
			// e.printStackTrace();
			// }
			// if (StrUtil.isNullOrEmpty(procInstId)) {
			// uecPunish.removeOnly();
			// ZkUtil.showError("流程发起失败！", "错误提示");
			// return;
			// }
			// // 第一审批人如果是发起人直接到人力审批环节
			// ApprovalUtil.isCurrentAssignNames(procInstId);
			// ZkUtil.showInformation("惩罚申请流程已发起", "提示信息");

		}
		if ("edit".equals(opType)) {
			PubUtil.fillPoFromBean(bean, uecPunish);
			dataTransToString();
			uecPunish.setModifier(currentName);
			uecPunish.setModifiedtime(DateUtil.getCurrenDate());
			if (!isOutStaffEntryExt) {
				uecPunish.updateOnly();
			}
			Events.postEvent("onOK", this.self, uecPunish);
			this.onCancel();
			Messagebox.show("更新成功！");
		}
		if ("approvalEdit".equals(opType)) {
			PubUtil.fillPoFromBean(bean, uecPunish);
			dataTransToString();
			uecPunish.setModifier(currentName);
			uecPunish.setModifiedtime(DateUtil.getCurrenDate());
			uecPunish.updateOnly();
			Events.postEvent("onOK", this.self, uecPunish);
			this.onCancel();
			Messagebox.show("更新成功！");
		}
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getUecPunishEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		if (StrUtil.isNullOrEmpty(bean.getPunishType().getValue())) {
			return "请填写惩罚类型";
		} else if (StrUtil.isNullOrEmpty(bean.getPunishOrg().getValue())) {
			return "请填写惩罚机构";
		} else if (StrUtil.isNullOrEmpty(bean.getPunishMatter().getValue())) {
			return "请填写惩罚事由";
		} else if (StrUtil.isNullOrEmpty(bean.getPunishDate().getValue())) {
			return "请填写惩罚日期";
		} else if (StrUtil.isNullOrEmpty(bean.getGlbdef4().getSelectedItem()
				.getValue())) {
			return "请选择处罚级别";
		} else if (StrUtil.isNullOrEmpty(bean.getPunishMeas().getValue())) {
			return "请填写处罚措施";
		}
		return null;
	}

	/**
	 * 一套模型，两套标准：日期类型需要格式化
	 */
	public void dataTransToString() {
		if (!StrUtil.isNullOrEmpty(bean.getPunishDate().getValue())) {
			uecPunish.setPunishDate(DateUtil.dateToStr(bean.getPunishDate()
					.getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getGlbdef2().getValue())) {
			uecPunish.setGlbdef2(DateUtil.dateToStr(bean.getGlbdef2()
					.getValue()));
		}
	}

	/**
	 * 将String类型的时间转换成Date类型
	 * 
	 * @throws ParseException
	 */
	public void stringTransToDate() throws ParseException {
		// 时间类型转换：String --> Date
		if (null != uecPunish.getPunishDate()) {
			bean.getPunishDate().setValue(
					DateUtil.patternStrToDate(uecPunish.getPunishDate()));
		}
		if (null != uecPunish.getGlbdef2()) {
			bean.getGlbdef2().setValue(
					DateUtil.patternStrToDate(uecPunish.getGlbdef2()));
		}
	}

}
